#coding=utf-8
from django import template
from datetime import datetime, timedelta
from django.core.serializers import serialize
from django.db.models.query import QuerySet
from django.utils.safestring import mark_safe
import json
register = template.Library()

@register.filter(name='dict_value')
def dict_value(ditc_, key):
    return ditc_.get(key,'')

@register.filter
def dates(value):
    if value is None:
        return ""
    now = datetime.now()
    delta = now-value
    if delta<timedelta(hours=1):
        return u'刚刚'
    elif delta<timedelta(hours=12):
        return u'今天'

@register.filter(name='rn2br')
def replace_rn2br(value):
    if value is None:
        return value
    return value.replace("\r\n", '<br/>').replace('\n','<br/>');


@register.filter
def jsonify(object):
    if isinstance(object, QuerySet):
        return mark_safe(serialize('json', object))
    return mark_safe(json.dumps(object, ensure_ascii=False))






